﻿/*****************************************************
 * 
 *          创建：lito
 * 
 *          时间：2016-01-28
 *          
 *          描述：1、增加文本框边框颜色属性
 *               2、增加文本得到焦点边框颜色属性
 *               3、增加文本框失去焦点边框颜色属性
 *               4、增加文本框文字提示输入信息属性
 *               5、增加文本框是否需要验证属性
 *               6、增加文本框需要验证时的验证规则（正则表达式）
 *               7、增加文本框支持下拉提示属性
 * 
 * ***************************************************/
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Diagnostics;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Drawing;
using System.Text.RegularExpressions;

namespace NF_Tools
{

    public partial class BF_TextBox : TextBox
    {
        public BF_TextBox()
        {
            InitializeComponent();

            this.BorderStyle = System.Windows.Forms.BorderStyle.FixedSingle;    //设置边框
            this.Font = new Font(@"微软雅黑", 9F);                                //设置文本默认字体及尺寸
            this.BorderStyle = System.Windows.Forms.BorderStyle.None;           //默认无边框
            this.ImeMode = System.Windows.Forms.ImeMode.NoControl;                //禁止使用键盘全角输入
        }


        /// <summary>
        /// 处理windows消息
        /// </summary>
        /// <param name="m">消息</param>
        protected override void WndProc(ref Message m)
        {
            base.WndProc(ref m);

            Pen p = new Pen(BorderColor, 1);
            Graphics grh = this.CreateGraphics();                                                   //GDI
            if(_BorderType == Style.SumLine)
            {
                grh.DrawLine(p, 0, this.Height - 10, 0, this.Height - 1);                               //left line
                grh.DrawLine(p, this.Width - 1, this.Height - 10, this.Width - 1, this.Height - 1);     //right line
                grh.DrawLine(p, 0, this.Height - 1, this.Width - 1, this.Height - 1);                   //down line
            }
            else if (_BorderType == Style.Line)
            {
                grh.DrawLine(p, 0, this.Height - 1, this.Width - 1, this.Height - 1);                   //down line
            }
            else
            {
                grh.DrawRectangle(p, 0, 0, this.Width - 1, this.Height - 1);
            }

            SetPiontText();
        }


        /// <summary>
        /// 得到输入焦点颜色
        /// </summary>
        protected override void OnGotFocus(EventArgs e)
        {
            base.OnGotFocus(e);
            BorderColor = HoverborderColor;

            ClearGDI();
        }


        /// <summary>
        /// 失去输入焦点颜色
        /// </summary>
        /// <param name="e"></param>
        protected override void OnLostFocus(EventArgs e)
        {
            base.OnLostFocus(e);
            BorderColor = LeaveborderColor;         //设置边框颜色
            if (string.IsNullOrEmpty(this.Text))
                this.Pointout = TagMeg;
        }


        /// <summary>
        /// 文本变化验证
        /// </summary>
        /// <param name="e"></param>
        protected override void OnTextChanged(EventArgs e)
        {
            base.OnTextChanged(e);
            ClearGDI();
            if (IsMust)                             //该项为必输项
            {
                if (!string.IsNullOrEmpty(this.Text))
                    CheckSuceese = true;
                else
                    CheckSuceese = false;
            }
            if (!string.IsNullOrEmpty(RgexMutch))
            {
                if (Regex.IsMatch(this.Text, RgexMutch) && CheckSuceese)
                    CheckSuceese = true;
                else
                    CheckSuceese = false;
            }

        }


        /// <summary>
        /// 添加提示文字
        /// </summary>
        private void SetPiontText()
        {
            if (!string.IsNullOrEmpty(Pointout))
            {
                Graphics grh = this.CreateGraphics();
                SolidBrush drawBrush = new SolidBrush(PointTextColor);
                grh.DrawString(Pointout, this.Font, drawBrush, new PointF(0, 0));
            }
        }

        /// <summary>
        /// 清空提示文字
        /// </summary>
        private void ClearGDI()
        {
            if (this.Pointout != "")
            {
                Graphics gs = this.CreateGraphics();
                gs.Clear(this.BackColor);
                TagMeg = this.Pointout;
                this.Pointout = "";
            }
        }


        private Color _LeaveborderColor = Color.Silver;
        private Color _HoverborderColor = Color.Orange;
        private Color _BorderColor = Color.Silver;
        private string _Pointout;
        private bool _IsMust = false;
        private string _RgexMutch;
        private bool _CheckSuceese = true;
        private List<string> _DataSoce;
        private Color _PointTextColor = Color.Gray;
        private Style _BorderType = Style.SumLine;

        


        [Browsable(true), Category("自定义属性"), Description("系统使用"), ReadOnly(true)]
        public string TagMeg { get; set; }

        /// <summary>
        /// 提示文字颜色
        /// </summary>
        [Browsable(true), Category("自定义属性"), Description("提示文字颜色")]
        public Color PointTextColor
        {
            get { return _PointTextColor; }
            set { _PointTextColor = value; }
        }

        /// <summary>
        /// 失去焦点颜色
        /// </summary>
        [Browsable(true), Category("自定义属性"), Description("设置控件的边框颜色(失去焦点时)")]
        public Color LeaveborderColor
        {
            get { return _LeaveborderColor; }
            set { _LeaveborderColor = value; }
        }

        /// <summary>
        /// 得到焦点颜色
        /// </summary>
        [Browsable(true), Category("自定义属性"), Description("设置控件的边框颜色(得到焦点时)")]
        public Color HoverborderColor
        {
            get { return _HoverborderColor; }
            set { _HoverborderColor = value; }
        }

        /// <summary>
        /// 边框颜色
        /// </summary>
        [Browsable(true), Category("自定义属性"), Description("设置控件的边框颜色")]
        public Color BorderColor
        {
            get { return _BorderColor; }
            set { _BorderColor = value; }
        }

        /// <summary>
        /// 文字提示
        /// </summary>
        [Browsable(true), Category("自定义属性"), Description("设置提示文字")]
        public string Pointout
        {
            get { return _Pointout; }
            set { _Pointout = value; }
        }

        /// <summary>
        /// 是否验证非空
        /// </summary>
        [Browsable(true), Category("自定义属性"), Description("提交时是否验证非空")]
        public bool IsMust
        {
            get { return _IsMust; }
            set
            {
                _IsMust = value;

                CheckSuceese = string.IsNullOrEmpty(base.Text) ? false : true;
            }
        }

        /// <summary>
        /// 正则表达式
        /// </summary>
        [Browsable(true), Category("自定义属性"), Description("验证的正则表达式,无需规则验证不输入.输入正则表达式不能带引号，例如：^[0-9]*$")]
        public string RgexMutch
        {
            get { return _RgexMutch; }
            set
            {
                _RgexMutch = value;
                if (!string.IsNullOrEmpty(value) && CheckSuceese)   //需要正则验证，并且已经通过非空验证
                {
                    if (!Regex.IsMatch(base.Text, RgexMutch))
                        CheckSuceese = true;
                    else
                        CheckSuceese = false;
                }
            }
        }

        /// <summary>
        /// 验证结果
        /// </summary>
        [Browsable(true), Category("自定义属性"), Description("验证是否通过：false未通过，ture通过"), ReadOnly(true)]
        public bool CheckSuceese
        {
            get { return _CheckSuceese; }
            set { _CheckSuceese = value; }
        }

        /// <summary>
        /// 下拉数据集合
        /// </summary>
        [Browsable(true), Category("自定义属性"), Description("文本搜索下拉数据集合")]
        public List<string> DataSoce
        {
            get { return _DataSoce; }
            set
            {
                _DataSoce = value;
                if (value != null)
                {
                    base.AutoCompleteCustomSource.Clear();
                    base.AutoCompleteCustomSource.AddRange(value.ToArray());
                    base.AutoCompleteMode = System.Windows.Forms.AutoCompleteMode.SuggestAppend;
                    base.AutoCompleteSource = System.Windows.Forms.AutoCompleteSource.CustomSource;
                }
            }
        }

        [Browsable(true), Category("自定义属性"), Description("文本框样式")]
        public Style BorderType
        {
            get { return _BorderType; }
            set { _BorderType = value; }
        }

        public enum Style 
        {
            Border,
            Line,
            SumLine
        }
    










    }
}
